package bond

import (
	"github.com/xuender/kit/types"
)

type _title uint8

const (
	Default _title = iota
	North
	Guangdong
)

// nolint: gochecknoglobals
var _num = [...]string{"大", "二", "三", "四", "五", "六", "七", "八", "九", "十"}

type Option struct {
	From int
	To   int
	Num  int
}

func (p _title) Title(bond Bond, opts ...Option) (string, error) {
	if len(opts) > 0 {
		if title, has := _complex[bond]; has {
			num := optionNum(opts)

			if opts[0].To < opts[0].From {
				return num + title[1], nil
			}

			return num + title[0], nil
		}
	}

	if title, has := _simple[bond]; has {
		return title, nil
	}

	return "", UndefinedError(bond)
}

func optionNum(opts []Option) string {
	if opts[0].To == opts[0].From {
		return ""
	}

	num := opts[0].To
	if opts[0].Num > 0 && num != opts[0].Num {
		num = opts[0].Num
	}

	if num <= len(_num) {
		return _num[num-1]
	}

	return types.Itoa(num)
}
